package com.pratices.proxy.cglib;

import com.pratices.proxy.target.Zhangsan;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.InvocationHandler;

import java.lang.reflect.Method;

public class CglibDynamicProxyApplication {
    /*
   CGlib动态代理代理类和委托类是子父关系
   * */
    public static void main(String[] args) {
        //创建委托类对象
        Zhangsan zhangSan = new Zhangsan();
        //创建对象
        Zhangsan zhangSanProxy = (Zhangsan) Enhancer.create(Zhangsan.class, new InvocationHandler() {
            @Override
            public Object invoke(Object o, Method method, Object[] objects) throws Throwable {
                Object invoke = method.invoke(zhangSan, objects);
                System.out.println("买了可乐");
                return invoke;
            }
        });
        zhangSanProxy.buyBreakfast("张三");
    }
}
